<?php

class Profile extends Controller {

    public $profile;
    public $userUpdate;
    public $error;
    public $day;
    public $month;
    public $year;

    public function __construct() {
        parent::__construct();
        $this->_initialize();
    }

    public function __destruct() {
        parent::__destruct();
    }

    public function index() {
        if ((bool) empty($this->_session['user'])) {
            header('Location:index.php?c=login');
        }

        if (isset($this->_post["user"])) {
            $this->userUpdate = $this->_post["user"];
            //unset($this->userUpdate['register']);
            $this->userUpdate['birthday'] = mktime(0, 0, 0, $this->userReg['month'], $this->userReg['day'], $this->userReg['year']);
            var_dump(date('d-m-Y', $this->userUpdate['birthday']));
            //Unset value not neccessary for create new user.
            foreach (array('day', 'month', 'year', 'register') as $val) {
            	unset($this->userUpdate[$val]);
            }
            $this->error = $this->user->updateProfile((object) $this->userUpdate);
            $this->_session = $_SESSION;
            $this->profile = $this->_session['user']['info'];
            $this->userUpdate = (object)$this->userUpdate;
        }

        $this->template('update_profile');
    }

    private function _initialize() {
        $this->profile = $this->_session['user']['info'];
        $this->model('user');
        $this->user->id = $this->profile->id;

        $this->error = '';
        $this->day[-1] = 'Day:';
        for($i = 1; $i <= 31; $i++){
        	$this->day[$i] = $i;
        }

        $this->month = array(
        		-1=>'Month:',
        		1=>'Jan',
        		2=>'Feb',
        		3=>'Mar',
        		4=>'Apr',
        		5=>'May',
        		6=>'Jun',
        		7=>'July',
        		8=>'Aug',
        		9=>'Sep',
        		10=>'Oct',
        		11=>'Nov',
        		12=>'Dec'
        );

        $currentYear = date('Y');
        $this->year[-1] = 'Year:';
        for ($i = $currentYear; $i >= 1905; $i--){
        	$this->year[$i] = $i;
        }
    }

    public function addavatar() {
        if (isset($this->_post['upload'])) {
            $this->user->username = $this->profile->username;
            $this->error = $this->user->updateAvatar();
        }
        //Update again session...
        $this->_session = $_SESSION;
        $this->profile = $this->_session['user']['info'];

        $this->template('add_avatar');
    }
}

?>